#include "MindVision.h"

MindVision::MindVision(){

    CameraSdkInit(1);
    iStatus=CameraEnumerateDevice(&cameraInform,&iCameraCounts);
    
    std::cout<<"iStatus="<<iStatus<<std::endl;
    std::cout<<"iCameraCount="<<iCameraCounts<<std::endl;
    
    if(iCameraCounts==0){
        return;
    }
    iStatus=CameraInit(&cameraInform,-1,-1,&hCamera);
    std::cout<<"iStatus="<<iStatus<<std::endl;
    if(iStatus!=CAMERA_STATUS_SUCCESS){
        return;
    }
    std::cout<<"MindVision init success"<<std::endl;
}

void MindVision::open(){
    
    CameraGetCapability(hCamera,&cameraSettings);

    //g_pRgbBuffer=new unsigned char(cameraSettings.sResolutionRange.iHeightMax*cameraSettings.sResolutionRange.iWidthMax*3);

    CameraPlay(hCamera);
 
    CameraSetIspOutFormat(hCamera,CAMERA_MEDIA_TYPE_BGR8);

}

void MindVision::getImage(cv::Mat & src){

    if(CameraGetImageBuffer(hCamera,&frameHead,&pbyBuffer,100)==CAMERA_STATUS_SUCCESS)
    {
        src=cv::Mat(frameHead.iHeight,frameHead.iWidth,CV_8UC3);

        CameraImageProcess(hCamera,pbyBuffer,src.data,&frameHead);
        
        CameraReleaseImageBuffer(hCamera,pbyBuffer);      
    }else{
        std::cout<<"MindVision::getImage() error"<<std::endl;
    }
}

void MindVision::close(){
    CameraUnInit(hCamera);
}

MindVision::~MindVision(){
    close();
}
